/*
 * 文件名：ProtocalSdtpDataAcceptServer.java
 * 版权：亚信联创版权所有
 * 描述：TODO
 * 修改人：xuwei3
 * 修改时间：2015-5-20
 * 修改内容：TODO
 */
package com.ailk.bigdata.etl.realstream.server.protocal.sdtp.netty.server;

import java.util.concurrent.TimeUnit;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.ReadTimeoutHandler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ailk.bigdata.etl.realstream.server.constant.ProtocalSdtpConstant;
import com.ailk.bigdata.etl.realstream.server.protocal.sdtp.netty.handler.HeartBeatRespHandler;
import com.ailk.bigdata.etl.realstream.server.protocal.sdtp.netty.handler.ProtocalLTEDataDecode;
import com.ailk.bigdata.etl.realstream.server.protocal.sdtp.netty.handler.ProtocalSdtpDataDecode;
import com.ailk.bigdata.etl.realstream.server.protocal.sdtp.netty.handler.ProtocalSdtpDataEncode;

/**
 * @description TODO
 * @author [xuwei3]
 * @version [版本号,2015-5-20]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class ProtocalSdtpDataAcceptServer
{
	private static Logger logger = LoggerFactory.getLogger(ProtocalSdtpDataAcceptServer.class);
	private int port;

	public ProtocalSdtpDataAcceptServer(int port)
	{
		this.port = port;
	}

	public void run(final ChannelInboundHandlerAdapter handler) throws Exception
	{
		EventLoopGroup bossGroup = new NioEventLoopGroup();
		EventLoopGroup workerGroup = new NioEventLoopGroup();
		try
		{
			ServerBootstrap server = new ServerBootstrap();
			server.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true)
					.option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_RCVBUF, 1048576 * 4).option(ChannelOption.AUTO_READ, true)
					.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
					.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT).handler(new LoggingHandler(LogLevel.WARN))
					.childHandler(new ChannelInitializer<SocketChannel>()
					{
						@Override
						public void initChannel(SocketChannel ch) throws Exception
						{
							ch.pipeline().addLast("byteToMess", new ProtocalSdtpDataDecode()).addLast("messToByte", new ProtocalSdtpDataEncode())
							// .addLast("readTimeoutHandler",
							// new
							// ReadTimeoutHandler(ProtocalSdtpConstant.SDTP_PROTOCAL_HEART_BEAT_INTERVAL,
							// TimeUnit.SECONDS))
									.addLast("heartBeatResp", new HeartBeatRespHandler()).addLast("handler", handler);
						}
					});
			server.bind(port).sync().channel().closeFuture().sync();
		}
		catch (Exception e)
		{
			logger.error("开启监听失败！端口[" + port + "]", e);
		}
		finally
		{
			bossGroup.shutdownGracefully();
			workerGroup.shutdownGracefully();
		}
	}

	public void runLTEServer(final ChannelInboundHandlerAdapter handler) throws Exception
	{

		EventLoopGroup bossGroup = new NioEventLoopGroup();
		EventLoopGroup workerGroup = new NioEventLoopGroup();
		try
		{
			ServerBootstrap server = new ServerBootstrap();
			server.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true)
					.option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_RCVBUF, 1048576 * 4).option(ChannelOption.AUTO_READ, true)
					.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
					.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT).handler(new LoggingHandler(LogLevel.WARN))
					.childHandler(new ChannelInitializer<SocketChannel>()
					{
						@Override
						public void initChannel(SocketChannel ch) throws Exception
						{
							ch.pipeline().addLast("byteToMess", new ProtocalLTEDataDecode()).addLast("messToByte", new ProtocalSdtpDataEncode())
							// .addLast("readTimeoutHandler",
							// new
							// ReadTimeoutHandler(ProtocalSdtpConstant.SDTP_PROTOCAL_HEART_BEAT_INTERVAL,
							// TimeUnit.SECONDS))
									.addLast("heartBeatResp", new HeartBeatRespHandler()).addLast("handler", handler);
						}
					});
			server.bind(port).sync().channel().closeFuture().sync();
		}
		catch (Exception e)
		{
			logger.error("开启监听失败！端口[" + port + "]", e);
		}
		finally
		{
			bossGroup.shutdownGracefully();
			workerGroup.shutdownGracefully();
		}

	}

}
